package com.five.controller.backstage;

import com.alibaba.fastjson.JSON;
import com.five.dao.backstage.GustDao;
import com.five.dao.impl.backstage.GustDaoImpl;
import com.five.entity.backstage.Gust;
import com.five.util.TransBean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 王登峰
 * @Description:
 * @Date: 2020/11/11 17:24
 */
@WebServlet("/gust")
@RestController
@RequestMapping("ggust")
public class GustController extends HttpServlet {
    GustDao gustDao = new GustDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String method = req.getParameter("method");
        if(method != null){
            switch (method){
                case "add":
                    add(req,resp);
                    break;
                case "del":
                    del(req,resp);
                    break;
                case "upd":
                    upd(req,resp);
                    break;
                case "recover":
                    recover(req,resp);
                    break;
            }
        }else {
            //获取页数和每页数据
            String page = req.getParameter("page");
            String limit = req.getParameter("limit");
            Map<String, String[]> parameterMap = req.getParameterMap();
            Gust gust = new Gust();
            TransBean.populate(gust,parameterMap);

            //获取数据总条数
            int i=gustDao.getcount(gust);
            List<Map> list = gustDao.selectGust(page,limit,gust);
            Map map = new HashMap();
            map.put("code",0);
            map.put("msg","");
            map.put("count",i);
            map.put("data",list);
            String s = JSON.toJSONString(map);
            PrintWriter out = resp.getWriter();
            out.write(s);
            out.flush();
            out.close();

        }
    }

    private void recover(HttpServletRequest req, HttpServletResponse resp) {

    }

    public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, String[]> parameterMap = req.getParameterMap();
        Gust gust = new Gust();
        TransBean.populate(gust,parameterMap);
        int i = gustDao.addGust(gust);
        System.out.println(i);
//        if(i>0){
//            resp.sendRedirect("/Hotel/view/gust.jsp");
//        }
    }

    public void del(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, String[]> parameterMap = req.getParameterMap();
        Gust gust = new Gust();
        TransBean.populate(gust,parameterMap);
        int i = gustDao.delGust(gust);
//        if(i>0){
//            resp.sendRedirect("/Hotel/view/gust.jsp");
//        }
    }

    public void upd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        Map<String, String[]> parameterMap = req.getParameterMap();
        Gust gust = new Gust();
        TransBean.populate(gust,parameterMap);
        int i = gustDao.update(gust);
//        System.out.println(i);
//        if(i>0){
//            resp.sendRedirect("/Hotel/view/gust.jsp");
//        }
    }


    @RequestMapping("bad")
    public int updata(Integer id){
        int i=gustDao.bad(id);
        if (i>0){
            return i;
        }
        return -1;
    }
    @RequestMapping("good")
    public int updatagood(Integer id){
        int i=gustDao.good(id);
        if (i>0){
            return i;
        }
        return -1;
    }

}
